package com.ego.admin.controller;

import com.ego.admin.po.SysRole;
import com.ego.admin.service.SysRoleService;
import com.ego.admin.vo.SysRoleSearchVo;
import com.ego.common.annotation.ControllerEndpoint;
import com.ego.common.constant.SystemConstant;
import com.ego.common.util.Result;
import com.ego.common.vo.PageVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: ego
 * @description: 角色
 * @author: KamTang
 * @create: 2023-02-23 19:46
 **/
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends Administrator {

    @Autowired
    private SysRoleService roleService;

    /**
     * 分页搜索
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody SysRoleSearchVo search) {
        PageVo<SysRole> page = roleService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 列表展示角色
     */
    @GetMapping("/list")
    public Result list() {
        List<SysRole> list = roleService.list();
        // 如果不是超级管理员，则只查询自己创建的角色列表
        if (getAdmin().getUserId() != SystemConstant.UserEnum.SUPER_ADMIN.getCode()){
            list = list.stream().map(role -> {
                if (role.getCreateUserId().equals(getAdmin().getUserId())) {
                    return role;
                } else {
                    return null;
                }
            }).collect(Collectors.toList());
        }
        return Result.ok().put("list", list);
    }

    /**
     * 新增角色
     */
    @ControllerEndpoint(exceptionMessage = "新增角色失败", operation = "新增角色")
    @PostMapping("/save")
    @RequiresPermissions("sys:role:save")
    public Result save (@RequestBody SysRole role) {
        role.setCreateUserId(getAdmin().getUserId());
        roleService.saveRole(role);
        return Result.ok();
    }

    /**
     * 更新
     */
    @ControllerEndpoint(exceptionMessage = "更新角色信息失败", operation = "更新角色信息")
    @PostMapping("/update")
    @RequiresPermissions("sys:role:update")
    public Result update (@RequestBody SysRole role) {
        role.setCreateUserId(getAdmin().getUserId());
        roleService.update(role);
        return Result.ok();
    }

    /**
     * 批量删除角色
     */
    @ControllerEndpoint(exceptionMessage = "批量删除角色失败", operation = "批量删除角色")
    @PostMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public Result deleteBatch (@RequestBody Long[] ids){
        roleService.deleteBatch(ids);
        return Result.ok();
    }

    /**
     * 根据角色id获取角色具体信息
     */
    @GetMapping("/getById/{roleId}")
    public Result get(@PathVariable("roleId") Long roleId) {
        return Result.ok().put("role", roleService.getRoleInfoById(roleId));
    }

    /**
     * 根据管理员ID获取角色信息
     */
    @GetMapping("/getByUserId/{userId}")
    public Result getByUserId(@PathVariable("userId") Long userId) {
        return Result.ok().put("role", roleService.getRoleInfoByUserId(userId));
    }

}
